package com.project2.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project2.bean.CommonData;
import com.project2.bean.bo.WxOrderCommentBO;
import com.project2.bean.bo.WxOrderMBO;
import com.project2.bean.bo.WxOrderSubmitBO;
import com.project2.bean.po.*;
import com.project2.bean.vo.*;
import com.project2.execption.BusinessException;
import com.project2.execption.Code;
import com.project2.execption.SystemException;
import com.project2.mapper.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: cc
 * @Date: 2022-08-14 17:41
 * @Description: 微信订单service
 */

@Service
public class WxOrderServiceImpl implements WxOrderService{

    @Autowired
    MarketOrderMapper orderMapper;

    @Autowired
    MarketOrderGoodsMapper orderGoodsMapper;

    @Autowired
    MarketCommentMapper commentMapper;

    @Autowired
    MarketCartMapper cartMapper;

    @Autowired
    MarketCouponUserMapper couponUserMapper;

    @Autowired
    MarketAddressMapper addressMapper;

    @Autowired
    MarketCouponMapper couponMapper;

    @Autowired
    MarketGoodsProductMapper goodsProductMapper;

    @Override
    public WxOrderSubmitVO submitOrder(WxOrderSubmitBO sbOrder, HttpServletRequest request) {

        String header = request.getHeader("X-CskaoyanMarket-Token");
        MarketUser user = (MarketUser) request.getSession().getAttribute(header);

        WxOrderSubmitVO submitVO = new WxOrderSubmitVO();


        //第一种 商品界面 直接购买  cartId=?
        if (sbOrder.getCartId() != 0){

            MarketCart mc = cartMapper.selectByPrimaryKey(sbOrder.getCartId());

            MarketAddress ad = addressMapper.selectByPrimaryKey(sbOrder.getAddressId());


            //扣库存
            MarketGoodsProduct product = new MarketGoodsProduct();
            product.setId(mc.getProductId());
            product.setNumber(mc.getNumber()+0);
            product.setUpdateTime(new Date());

//           数量需要处理异常
            try {
                int i = goodsProductMapper.updateProductNumByP(product);
            } catch (Exception e) {
                throw new SystemException(Code.WX_ORDER_SUBMIT_ERR,e);

            }


            //存入一个order
            MarketOrder order = new MarketOrder();
            order.setUserId(user.getId());
            //??
            long orderSn = System.currentTimeMillis();
            order.setOrderSn(orderSn+ "");

            order.setOrderStatus((short) 101);

            //通过address去取收货地址电话号码等详细信息
            order.setConsignee(ad.getName());
            order.setMobile(ad.getTel());
            order.setAddress(ad.getAddressDetail());
            order.setMessage(sbOrder.getMessage());

            order.setFreightPrice(BigDecimal.valueOf(10));


            //查询优惠卷金额   联合查询记得回来练习

            //有优惠卷
            MarketCoupon marketCoupon = null;
            Double money = 0.0;
            if(sbOrder.getUserCouponId()  <= 0) {
                MarketCouponUser marketCouponUser = couponUserMapper.selectByPrimaryKey(sbOrder.getUserCouponId());

                marketCoupon = couponMapper.selectByPrimaryKey(marketCouponUser.getCouponId());
                BigDecimal couponMoney = BigDecimal.valueOf(marketCoupon.getDiscount());
                order.setGrouponPrice(couponMoney);
                money = mc.getPrice().intValue() + 10.00 - marketCoupon.getDiscount();
            }
             //没有使用优惠卷
            money = mc.getPrice().intValue() * mc.getNumber() + 10.00;
             //10.00自定义邮费
            order.setOrderPrice(BigDecimal.valueOf(money));
            order.setActualPrice(BigDecimal.valueOf(money)); //积分金额无用 所以-0

            BigDecimal couponPrice = BigDecimal.valueOf(0.00);  //无优惠卷金额  设为0
            order.setCouponPrice(couponPrice);

            order.setGoodsPrice(mc.getPrice());
            order.setPayTime(new Date());
            order.setAddTime(new Date());
            order.setUpdateTime(new Date());
            order.setComments(mc.getNumber());
            order.setId(null);


            BigDecimal grouponPrice = BigDecimal.valueOf(0.00);  //团购价格无用
            order.setGrouponPrice(grouponPrice);

            BigDecimal integralPrice = BigDecimal.valueOf(0.00);  //积分价格无用
            order.setIntegralPrice(integralPrice);
            int insertO = orderMapper.insertSelective(order);


                //分别存入orderGoods表
                MarketOrderGoods orderGoods = new MarketOrderGoods();
                orderGoods.setOrderId(order.getId());
                orderGoods.setGoodsId(mc.getGoodsId());
                orderGoods.setGoodsName(mc.getGoodsName());
                orderGoods.setGoodsSn(mc.getGoodsSn());
                orderGoods.setProductId(mc.getProductId());
                orderGoods.setNumber(mc.getNumber());
                orderGoods.setPrice(mc.getPrice());
                orderGoods.setSpecifications(mc.getSpecifications());
                orderGoods.setPicUrl(mc.getPicUrl());
                orderGoods.setAddTime(new Date());
                orderGoods.setUpdateTime(new Date());
                orderGoods.setDeleted(false);

                int insertOG = orderGoodsMapper.insertSelective(orderGoods);

            if (sbOrder.getUserCouponId() != -1) {
                //下完订单 把逻辑删除 用户优惠卷
                MarketCouponUser couponUser = new MarketCouponUser();
                couponUser.setId(sbOrder.getUserCouponId());
                couponUser.setStatus((short) 1);
                couponUser.setUpdateTime(new Date());
                couponUser.setUserId(user.getId());
                // if (sbOrder.getCartId()!=0){
                //     couponUser.setCouponId(sbOrder.getCouponId());
                // }else {
                //     couponUser.setCouponId(0);
                // }

                couponUserMapper.updateByPrimaryKeySelective(couponUser);

                couponUserMapper.updateByPrimaryKey(couponUser);


            }

            //将对应cart 逻辑删除
            MarketCart cart = new MarketCart();
            cart.setId(mc.getId());
            cart.setDeleted(true);
            cartMapper.updateByPrimaryKey(cart);

            submitVO.setOrderId(order.getId());
            submitVO.setGrouponLinkId(sbOrder.getGrouponLinkId());

            return submitVO;

            }





        //第二种  购物车下单 cartId 永远等于0
        //直接去cart数据库去通过 check 为1 和 delete = 0  统计出本次勾选的商品计算金额 和数量
        MarketCartExample example = new MarketCartExample();
        MarketCartExample.Criteria criteria = example.createCriteria();
        criteria.andCheckedEqualTo(true);
        criteria.andDeletedEqualTo(false);

        List<MarketCart> goodsList = cartMapper.selectByExample(example);



        //统计此次订单有多少件货  更新还未评价商品的总数
        double goodsMoney = 0; //每一种类的小总金额
        // BigDecimal moneyCount = 0;
        double  moneyCount = 0;  //统计订单所有商品总金额
        int goodsNum = 0;
        for (MarketCart good : goodsList) {
            int num = good.getNumber();
            goodsNum += num;
            goodsMoney = good.getPrice().doubleValue()* num;

            moneyCount += goodsMoney;
            // Numcount += good.getNumber();
            // moneyCount = good.getPrice();
        }

        MarketAddress ad = addressMapper.selectByPrimaryKey(sbOrder.getAddressId());

        //先扣除商品数量再存入订单
        for (MarketCart cart : goodsList) {

            MarketGoodsProduct product = new MarketGoodsProduct();
            product.setId(cart.getProductId());
            product.setNumber(cart.getNumber()+0);
            product.setUpdateTime(new Date());

//           数量需要处理异常

            try {
                int i = goodsProductMapper.updateProductNumByP(product);
            } catch (Exception e) {
                throw new SystemException(Code.WX_ORDER_SUBMIT_ERR,e);

            }

        }


        //存入一个order
        MarketOrder order = new MarketOrder();
        order.setUserId(user.getId());
        //??
        long orderSn = System.currentTimeMillis();
        order.setOrderSn(orderSn+ "");

        order.setOrderStatus((short) 101);

        //通过address去取收货地址电话号码等详细信息
        order.setConsignee(ad.getName());
        order.setMobile(ad.getTel());
        order.setAddress(ad.getAddressDetail());
        order.setMessage(sbOrder.getMessage());

         long payId = System.currentTimeMillis();
        order.setFreightPrice(BigDecimal.valueOf(10));
        order.setPayId(payId+"");
        
        //查询优惠卷金额   联合查询记得回来练习

        //有优惠卷
        MarketCoupon marketCoupon = null;
        Double money = 0.0;
        if(sbOrder.getUserCouponId() <= 0) {
            MarketCouponUser marketCouponUser = couponUserMapper.selectByPrimaryKey(sbOrder.getUserCouponId());

            marketCoupon = couponMapper.selectByPrimaryKey(marketCouponUser.getCouponId());
            BigDecimal couponMoney = BigDecimal.valueOf(marketCoupon.getDiscount());
            order.setGrouponPrice(couponMoney);
            money = moneyCount + 10.00 - marketCoupon.getDiscount();
        }
        // order.setGrouponPrice(BigDecimal.valueOf(marketCoupon.getDiscount()));

        //无优惠卷

        BigDecimal couponPrice = BigDecimal.valueOf(0.00);  //无优惠卷金额  设为0
        order.setCouponPrice(couponPrice);
        Double i = moneyCount + 10.00;
        order.setOrderPrice(BigDecimal.valueOf(i));
        order.setActualPrice(BigDecimal.valueOf(i));


        BigDecimal goodsPrice = BigDecimal.valueOf(moneyCount);

        order.setGoodsPrice(goodsPrice);

        order.setPayTime(new Date());
        order.setAddTime(new Date());
        order.setUpdateTime(new Date());
        order.setComments((short) goodsNum);
        order.setId(null);


        BigDecimal grouponPrice = BigDecimal.valueOf(0.00);  //团购价格无用
        order.setGrouponPrice(grouponPrice);

        BigDecimal integralPrice = BigDecimal.valueOf(0.00);  //积分价格无用
        order.setIntegralPrice(integralPrice);

        int insertO = orderMapper.insertSelective(order);




        int insertOG = 0;
        for (MarketCart mc : goodsList) {

            //分别存入orderGoods表
            MarketOrderGoods orderGoods = new MarketOrderGoods();
            orderGoods.setOrderId(order.getId());
            orderGoods.setGoodsId(mc.getGoodsId());
            orderGoods.setGoodsName(mc.getGoodsName());
            orderGoods.setGoodsSn(mc.getGoodsSn());
            orderGoods.setProductId(mc.getProductId());
            orderGoods.setNumber(mc.getNumber());
            orderGoods.setPrice(mc.getPrice());
            orderGoods.setSpecifications(mc.getSpecifications());
            orderGoods.setPicUrl(mc.getPicUrl());
            orderGoods.setAddTime(new Date());
            orderGoods.setUpdateTime(new Date());
            orderGoods.setDeleted(false);

            insertOG = orderGoodsMapper.insertSelective(orderGoods);

            //将对应cart 逻辑删除
            MarketCart cart = new MarketCart();
            cart.setId(mc.getId());
            cart.setDeleted(true);
            cartMapper.updateByPrimaryKey(cart);
        }


        if (sbOrder.getUserCouponId() != -1) {
            //下完订单 把逻辑删除 用户优惠卷
            MarketCouponUser couponUser = new MarketCouponUser();
            couponUser.setId(sbOrder.getUserCouponId());
            couponUser.setStatus((short) 1);
            couponUser.setUserId(user.getId());
            // if (sbOrder.getCartId()!=0){
            //     couponUser.setCouponId(sbOrder.getCouponId());
            // }else {
            //     couponUser.setCouponId(0);
            // }
            couponUser.setUpdateTime(new Date());
            couponUserMapper.updateByPrimaryKey(couponUser);

        }


        submitVO.setOrderId(order.getId());
        submitVO.setGrouponLinkId(sbOrder.getGrouponLinkId());

        return submitVO;
    }

    @Override
    public CommonData orderList(Integer showType, Integer page, Integer limit,HttpServletRequest request) {


        String header = request.getHeader("X-CskaoyanMarket-Token");
        MarketUser user = (MarketUser) request.getSession().getAttribute(header);

        if (user==null){

            throw new BusinessException(501,"请登录");
            // return ResultVo.ok("请登录",501);
        }

        PageHelper.startPage(page, limit);
        //总的WxOrderListVO 需要塞入CommonData中

        // WxOrderListVO wxOrderListVO = new WxOrderListVO();
        List<WxOrderListInnerVO> listInnerVOS = new ArrayList<>();

        //显示全部订单  1待付款（101）   2.代发货（201） 3.待收货（301） 4.待评价（401）
       if (showType == 0){


           // MarketOrderExample example = new MarketOrderExample();
           // MarketOrderExample.Criteria criteria = example.createCriteria();
           // criteria.andOrderStatusEqualTo(status);

           List<MarketOrder> list = orderMapper.selectAllStatusBystatus(user.getId());

           for (MarketOrder o: list) {
               WxOrderListInnerVO InnerVO = new WxOrderListInnerVO();
               InnerVO.setActualPrice(o.getActualPrice());
               InnerVO.setAftersaleStatus(o.getAftersaleStatus());


               //根据orderId去marketordergood表去查
               MarketOrderGoodsExample orderGoodsExample = new MarketOrderGoodsExample();
               MarketOrderGoodsExample.Criteria exampleCriteria = orderGoodsExample.createCriteria();
               exampleCriteria.andOrderIdEqualTo(o.getId());
               List<MarketOrderGoods> marketOrderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);
               InnerVO.setGoodsList(marketOrderGoods);


               //handleOption
               WxOrderDetailInnerInfoOpVO InnerInfoOpVO = new WxOrderDetailInnerInfoOpVO();

               if (o.getOrderStatus() == 101) {
                   InnerInfoOpVO.setAftersale(false);
                   InnerInfoOpVO.setCancel(true);
                   InnerInfoOpVO.setComment(false);
                   InnerInfoOpVO.setConfirm(false);
                   InnerInfoOpVO.setDelete(false);
                   InnerInfoOpVO.setPay(true);
                   InnerInfoOpVO.setRebuy(false);
                   InnerInfoOpVO.setRefund(false);
               }
               if (o.getOrderStatus() == 102) {
                   InnerInfoOpVO.setAftersale(false);
                   InnerInfoOpVO.setCancel(false);
                   InnerInfoOpVO.setComment(false);
                   InnerInfoOpVO.setConfirm(false);
                   InnerInfoOpVO.setDelete(true);
                   InnerInfoOpVO.setPay(false);
                   InnerInfoOpVO.setRebuy(false);
                   InnerInfoOpVO.setRefund(false);
               }
               if (o.getOrderStatus() == 201) {
                   InnerInfoOpVO.setAftersale(false);
                   InnerInfoOpVO.setCancel(false);
                   InnerInfoOpVO.setComment(false);
                   InnerInfoOpVO.setConfirm(false);
                   InnerInfoOpVO.setDelete(false);
                   InnerInfoOpVO.setPay(false);
                   InnerInfoOpVO.setRebuy(false);
                   InnerInfoOpVO.setRefund(true);
               }
               if (o.getOrderStatus() == 202 ||o.getOrderStatus() == 203) {
                   InnerInfoOpVO.setAftersale(false);
                   InnerInfoOpVO.setCancel(false);
                   InnerInfoOpVO.setComment(false);
                   InnerInfoOpVO.setConfirm(false);
                   InnerInfoOpVO.setDelete(false);
                   InnerInfoOpVO.setPay(false);
                   InnerInfoOpVO.setRebuy(false);
                   InnerInfoOpVO.setRefund(false);
               }

               if (o.getOrderStatus() == 301) {
                   InnerInfoOpVO.setAftersale(false);
                   InnerInfoOpVO.setCancel(false);
                   InnerInfoOpVO.setComment(false);
                   InnerInfoOpVO.setConfirm(true);
                   InnerInfoOpVO.setDelete(false);
                   InnerInfoOpVO.setPay(false);
                   InnerInfoOpVO.setRebuy(false);
                   InnerInfoOpVO.setRefund(false);
               }

               if (o.getOrderStatus() == 401) {
                   InnerInfoOpVO.setAftersale(true);
                   InnerInfoOpVO.setCancel(false);
                   InnerInfoOpVO.setComment(true);
                   InnerInfoOpVO.setConfirm(false);
                   InnerInfoOpVO.setDelete(true);
                   InnerInfoOpVO.setPay(false);
                   InnerInfoOpVO.setRebuy(true);
                   InnerInfoOpVO.setRefund(false);
               }




               InnerVO.setHandleOption(InnerInfoOpVO);


               InnerVO.setId(o.getId());

               if (o.getGrouponPrice() == null) {
                   InnerVO.setIsGroupin(false);
               }
               InnerVO.setOrderSn(o.getOrderSn());


               String orderStatusText = null;
               if (o.getOrderStatus() == 101){
                   orderStatusText = "未付款";
               }
               if (o.getOrderStatus() == 201){
                   orderStatusText = "已付款";
               }
               if (o.getOrderStatus() == 202){
                   orderStatusText = "订单取消，申请退款中";
               }
               if (o.getOrderStatus() == 203){
                   orderStatusText = "已退款";
               }
               if (o.getOrderStatus() == 301){
                   orderStatusText = "已发货";
               }
               if (o.getOrderStatus() == 401){
                   orderStatusText = "已收货";
               }
               if (o.getOrderStatus() == 102){
                   orderStatusText = "用户已取消";
               }

               InnerVO.setOrderStatusText(orderStatusText);
               listInnerVOS.add(InnerVO);

           }

               PageInfo<WxOrderListInnerVO> pageInfo = new PageInfo<>(listInnerVOS);
                 return CommonData.data(pageInfo);


       }


       short status = 101;
       if (showType == 1 ){
           status = 101;
       }
       if (showType == 2) {
           status = 201;
       }
       if (showType == 3){
           status = 301;
       }
       if (showType == 4){
           status = 401;
       }

       //让status作为参数传入其中


        MarketOrderExample example = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(user.getId());
        criteria.andOrderStatusEqualTo(status);
        criteria.andDeletedEqualTo(false);
        List<MarketOrder> list = orderMapper.selectByExample(example);

        for (MarketOrder o: list) {
            WxOrderListInnerVO InnerVO = new WxOrderListInnerVO();
             InnerVO.setActualPrice(o.getActualPrice());
             InnerVO.setAftersaleStatus(o.getAftersaleStatus());


             //根据orderId去marketordergood表去查
            MarketOrderGoodsExample orderGoodsExample = new MarketOrderGoodsExample();
            MarketOrderGoodsExample.Criteria exampleCriteria = orderGoodsExample.createCriteria();
            exampleCriteria.andOrderIdEqualTo(o.getId());
            List<MarketOrderGoods> marketOrderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);
            InnerVO.setGoodsList(marketOrderGoods);


            //handleOption
            WxOrderDetailInnerInfoOpVO InnerInfoOpVO = new WxOrderDetailInnerInfoOpVO();


            if (o.getOrderStatus() == 101) {
                InnerInfoOpVO.setAftersale(false);
                InnerInfoOpVO.setCancel(true);
                InnerInfoOpVO.setComment(false);
                InnerInfoOpVO.setConfirm(false);
                InnerInfoOpVO.setDelete(false);
                InnerInfoOpVO.setPay(true);
                InnerInfoOpVO.setRebuy(false);
                InnerInfoOpVO.setRefund(false);
            }
            if (o.getOrderStatus() == 102) {
                InnerInfoOpVO.setAftersale(false);
                InnerInfoOpVO.setCancel(false);
                InnerInfoOpVO.setComment(false);
                InnerInfoOpVO.setConfirm(false);
                InnerInfoOpVO.setDelete(true);
                InnerInfoOpVO.setPay(false);
                InnerInfoOpVO.setRebuy(false);
                InnerInfoOpVO.setRefund(false);
            }
            if (o.getOrderStatus() == 201) {
                InnerInfoOpVO.setAftersale(false);
                InnerInfoOpVO.setCancel(false);
                InnerInfoOpVO.setComment(false);
                InnerInfoOpVO.setConfirm(false);
                InnerInfoOpVO.setDelete(false);
                InnerInfoOpVO.setPay(false);
                InnerInfoOpVO.setRebuy(false);
                InnerInfoOpVO.setRefund(true);
            }
            if (o.getOrderStatus() == 202 ||o.getOrderStatus() == 203) {
                InnerInfoOpVO.setAftersale(false);
                InnerInfoOpVO.setCancel(false);
                InnerInfoOpVO.setComment(false);
                InnerInfoOpVO.setConfirm(false);
                InnerInfoOpVO.setDelete(false);
                InnerInfoOpVO.setPay(false);
                InnerInfoOpVO.setRebuy(false);
                InnerInfoOpVO.setRefund(false);
            }

            if (o.getOrderStatus() == 301) {
                InnerInfoOpVO.setAftersale(false);
                InnerInfoOpVO.setCancel(false);
                InnerInfoOpVO.setComment(false);
                InnerInfoOpVO.setConfirm(true);
                InnerInfoOpVO.setDelete(false);
                InnerInfoOpVO.setPay(false);
                InnerInfoOpVO.setRebuy(false);
                InnerInfoOpVO.setRefund(false);
            }

            if (o.getOrderStatus() == 401) {
                InnerInfoOpVO.setAftersale(true);
                InnerInfoOpVO.setCancel(false);
                InnerInfoOpVO.setComment(true);
                InnerInfoOpVO.setConfirm(false);
                InnerInfoOpVO.setDelete(true);
                InnerInfoOpVO.setPay(false);
                InnerInfoOpVO.setRebuy(true);
                InnerInfoOpVO.setRefund(false);
            }


            //去数据库查该用户是否购买过同id的商品
            //
            // MarketOrderExample orderExample = new MarketOrderExample();
            // MarketOrderExample.Criteria criteria1 = orderExample.createCriteria();
            // criteria1.and
            // orderMapper.selectByExample(1)



            //暂时不知道 怎么判断是否重新是不是第二次买


            InnerVO.setHandleOption(InnerInfoOpVO);




            InnerVO.setId(o.getId());

            if (o.getGrouponPrice() == null){
                InnerVO.setIsGroupin(false);
            }
            InnerVO.setOrderSn(o.getOrderSn());


            String orderStatusText = null;
            if (o.getOrderStatus() == 101){
                orderStatusText = "未付款";
            }
            if (o.getOrderStatus() == 201){
                orderStatusText = "已付款";
            }
            if (o.getOrderStatus() == 202){
                orderStatusText = "订单取消，申请退款中";
            }
            if (o.getOrderStatus() == 203){
                orderStatusText = "已退款";
            }
            if (o.getOrderStatus() == 301){
                orderStatusText = "已发货";
            }
            if (o.getOrderStatus() == 401){
                orderStatusText = "已收货";
            }
            if (o.getOrderStatus() == 102){
                orderStatusText = "用户已取消";
            }

            InnerVO.setOrderStatusText(orderStatusText);
            listInnerVOS.add(InnerVO);

        }



        PageInfo<WxOrderListInnerVO> pageInfo = new PageInfo<>(listInnerVOS);
        return CommonData.data(pageInfo);

    }



    @Override
    public int confirmOrder(WxOrderMBO wxOrder) {

        MarketOrder order = new MarketOrder();
        order.setId(wxOrder.getOrderId());
        order.setUpdateTime(new Date());
        order.setOrderStatus((short) 401);
        order.setConfirmTime(new Date());

        int update =  orderMapper.updateByPrimaryKeySelective(order);

        return update;
    }

    @Override
    public int refundOrder(WxOrderMBO wxOrder) {

        MarketOrder order = new MarketOrder();
        order.setId(wxOrder.getOrderId());
        order.setOrderStatus((short) 202);
        order.setUpdateTime(new Date());

        int update = orderMapper.updateByPrimaryKeySelective(order);

        return update;
    }

    @Override
    public int deleteOrder(WxOrderMBO wxOrderMBO) {

        MarketOrder order = new MarketOrder();
        order.setId(wxOrderMBO.getOrderId());
        order.setDeleted(true);
        order.setUpdateTime(new Date());

        int delete = orderMapper.updateByPrimaryKeySelective(order);

        return delete;
    }

    @Override
    public  List<MarketOrderGoods> goodsInfo(Integer orderId, Integer goodsId) {


        MarketOrderGoodsExample example = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        criteria.andGoodsIdEqualTo(goodsId);


        List<MarketOrderGoods> marketOrderGoods = orderGoodsMapper.selectByExample(example);

        return marketOrderGoods;
    }

    @Override
    public WxOrderDetailVO orderDetail(Integer orderId) {

        MarketOrderGoodsExample example = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId);

        List<MarketOrderGoods> marketOrderGoods = orderGoodsMapper.selectByExample(example);


        MarketOrder o = orderMapper.selectByPrimaryKey(orderId);


        WxOrderDetailInnerInfoOpVO InnerInfoOpVO = new WxOrderDetailInnerInfoOpVO();

        // MarketOrderExample orderExample = new MarketOrderExample();
        // MarketOrderExample.Criteria criteria1 = orderExample.createCriteria();
        // criteria1.and
        // orderMapper.selectByExample(1)
        if (o.getOrderStatus() == 101) {
            InnerInfoOpVO.setAftersale(false);
            InnerInfoOpVO.setCancel(true);
            InnerInfoOpVO.setComment(false);
            InnerInfoOpVO.setConfirm(false);
            InnerInfoOpVO.setDelete(false);
            InnerInfoOpVO.setPay(true);
            InnerInfoOpVO.setRebuy(false);
            InnerInfoOpVO.setRefund(false);
        }
        if (o.getOrderStatus() == 102) {
            InnerInfoOpVO.setAftersale(false);
            InnerInfoOpVO.setCancel(false);
            InnerInfoOpVO.setComment(false);
            InnerInfoOpVO.setConfirm(false);
            InnerInfoOpVO.setDelete(true);
            InnerInfoOpVO.setPay(false);
            InnerInfoOpVO.setRebuy(false);
            InnerInfoOpVO.setRefund(false);
        }
        if (o.getOrderStatus() == 201) {
            InnerInfoOpVO.setAftersale(false);
            InnerInfoOpVO.setCancel(false);
            InnerInfoOpVO.setComment(false);
            InnerInfoOpVO.setConfirm(false);
            InnerInfoOpVO.setDelete(false);
            InnerInfoOpVO.setPay(false);
            InnerInfoOpVO.setRebuy(false);
            InnerInfoOpVO.setRefund(true);
        }
        if (o.getOrderStatus() == 202 ||o.getOrderStatus() == 203) {
            InnerInfoOpVO.setAftersale(false);
            InnerInfoOpVO.setCancel(false);
            InnerInfoOpVO.setComment(false);
            InnerInfoOpVO.setConfirm(false);
            InnerInfoOpVO.setDelete(false);
            InnerInfoOpVO.setPay(false);
            InnerInfoOpVO.setRebuy(false);
            InnerInfoOpVO.setRefund(false);
        }

        if (o.getOrderStatus() == 301) {
            InnerInfoOpVO.setAftersale(false);
            InnerInfoOpVO.setCancel(false);
            InnerInfoOpVO.setComment(false);
            InnerInfoOpVO.setConfirm(true);
            InnerInfoOpVO.setDelete(false);
            InnerInfoOpVO.setPay(false);
            InnerInfoOpVO.setRebuy(false);
            InnerInfoOpVO.setRefund(false);
        }

        if (o.getOrderStatus() == 401) {
            InnerInfoOpVO.setAftersale(true);
            InnerInfoOpVO.setCancel(false);
            InnerInfoOpVO.setComment(true);
            InnerInfoOpVO.setConfirm(false);
            InnerInfoOpVO.setDelete(true);
            InnerInfoOpVO.setPay(false);
            InnerInfoOpVO.setRebuy(true);
            InnerInfoOpVO.setRefund(false);
        }




        //查询快递名称
        String shipName = orderMapper.selectShipNameByCode(o.getShipChannel());

        WxOrderDetailInnerInfoVO InnerInfoVO = new WxOrderDetailInnerInfoVO();
        InnerInfoVO.setActualPrice(o.getActualPrice());
        InnerInfoVO.setAddTime(o.getAddTime());
        InnerInfoVO.setAftersaleStatus(o.getAftersaleStatus());

        InnerInfoVO.setHandleOption(InnerInfoOpVO);

        InnerInfoVO.setConsignee(o.getConsignee());
        InnerInfoVO.setCouponPrice(o.getCouponPrice());
        InnerInfoVO.setExpCode(o.getShipChannel());
        InnerInfoVO.setExpName(shipName);
        InnerInfoVO.setExpCode(o.getShipChannel());
        InnerInfoVO.setFreightPrice(o.getFreightPrice());
        InnerInfoVO.setId(o.getId());
        InnerInfoVO.setMessage(o.getMessage());
        InnerInfoVO.setMobile(o.getMobile());
        InnerInfoVO.setOrderSn(o.getOrderSn());
        InnerInfoVO.setGoodsPrice(o.getOrderPrice());
        InnerInfoVO.setAddress(o.getAddress());

        String StatusText = null;
        if (o.getOrderStatus() == 101){
            StatusText = "未付款";
        }else if (o.getOrderStatus() == 201){
            StatusText = "已付款";
        }else if (o.getOrderStatus() == 202){
            StatusText = "订单取消，申请退款中";
        }else if (o.getOrderStatus() == 203){
            StatusText = "已退款";
        }else if (o.getOrderStatus() == 301){
            StatusText = "已发货";
        }else if (o.getOrderStatus() == 401){
            StatusText = "已收货";
        }else if (o.getOrderStatus() == 102){
            StatusText = "用户已取消";
        }

        InnerInfoVO.setOrderStatusText(StatusText);



        //汇总


        WxOrderDetailVO Detail = new WxOrderDetailVO();
        Detail.setExpressInfo(null);
        Detail.setOrderGoods(marketOrderGoods);
        Detail.setOrderInfo(InnerInfoVO);

        return Detail;
    }

    @Override
    public int addComment(WxOrderCommentBO bO,Integer userId) {

    //先更新comment表

        MarketOrderGoods oG = orderGoodsMapper.selectByPrimaryKey(bO.getOrderGoodsId());

        MarketComment comment = new MarketComment();
        comment.setValueId(0);
        comment.setContent(bO.getContent());
        comment.setUserId(userId);
        if (bO.getPicUrls() == null){
            comment.setHasPicture(false);
        }
        comment.setHasPicture(true);
        //需要以json的形式存入数据库 需要检查！！
        comment.setPicUrls(bO.getPicUrls());
        comment.setStar(bO.getStar());
        comment.setAddTime(new Date());
        comment.setUpdateTime(new Date());

        int insert = commentMapper.insertSelective(comment);

        //取出commentId  更新时间再放入ordergoods表
        //订单商品评论，如果是-1，则超期不能评价；如果是0，则可以评价；如果其他值，则是comment表里面的评论ID。

        MarketOrderGoods orderGoods = new MarketOrderGoods();
        orderGoods.setComment(comment.getId());
        orderGoods.setUpdateTime(new Date());

        int num = orderGoodsMapper.insertSelective(orderGoods);


         //order表 待评价订单商品数量

        //商品的数量  去order表未评论表总数减去商品的数量
        Short number = oG.getNumber();
        Date date = new Date();
        int update =  orderMapper.updateOrderCommentById(oG.getOrderId(),number,date);

        if (insert == 1 && num ==1 && update == 1){
            return 1;
        }
        return 0;
    }

    @Override
    public int cancelOrder(WxOrderMBO wxOrderMBO) {


        //更新订单
        MarketOrder order = new MarketOrder();
        order.setId(wxOrderMBO.getOrderId());
        order.setOrderStatus((short) 102);
        order.setUpdateTime(new Date());

        int num = orderMapper.updateByPrimaryKeySelective(order);
        //更新商品数量+1

        //1.搜索出一个订单的所有List的
        MarketOrderGoodsExample example = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(wxOrderMBO.getOrderId());
        List<MarketOrderGoods> marketOrderGoods = orderGoodsMapper.selectByExample(example);

        int num2 = 0;
        for (MarketOrderGoods og : marketOrderGoods) {

            // String s = og.getSpecifications();
            // StringBuffer sb = new StringBuffer(s);
            // sb.insert(0,'[');
            // sb.insert(sb.length()-1,"]");

            MarketGoodsProduct product = new MarketGoodsProduct();
            product.setGoodsId(og.getGoodsId());

            product.setSpecifications(og.getSpecifications());
            product.setNumber(og.getNumber()+0);
            product.setUpdateTime(new Date());

//           数量需要处理异常
         num2 = goodsProductMapper.updateDeleteProductNumByP(product);
         if (num2 != 1){
             break;
         }
        }

        if(num == 1 && num2 == 1){
            return 1;
        }
        return 0;
    }

    @Override
    public int payOrder(Integer orderId) {

        // MarketOrderExample example = new MarketOrderExample();
        // MarketOrderExample.Criteria criteria = example.createCriteria();
        // criteria.andIdEqualTo(orderId);
        // criteria.andp


        MarketOrder order = new MarketOrder();
        order.setId(orderId);
        order.setPayTime(new Date());
        order.setOrderStatus((short)201);


        long payId = System.currentTimeMillis();
        order.setPayId(payId+"");

        order.setUpdateTime(new Date());

        int i = orderMapper.updateByPrimaryKeySelective(order);

        return i;
    }
}
